package com.livery.data.creators;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.internal.jaxb.mapping.orm.JaxbDiscriminatorType;

import com.livery.app.main.HibernateUtil;
import com.livery.data.Client;

public class ClientCreator extends JDialog implements WindowListener,ActionListener,Creator<Client>{
	private Client object;
	private boolean objectSet = false;
	private JTextField clientNameField;
	private JTextField clientSurnameField;
	private JTextField clientPhoneField;
	private JTextField clientMailField;
	private JTextField clientPinField;
	private JTextField clientDocumentId;
	private JTextField clientCityField;
	private JTextField clientPostalCode;
	private JTextField clientStreetField;
	private JButton btnZamknij;
	private JButton btnZapisz;
	private JButton btnZapiszIZamknij;
	
	public ClientCreator() {
		this.setModal(true);
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{196, 228, 0};
		gridBagLayout.rowHeights = new int[]{67, 67, 67, 67, 67, 67, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 2.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		this.setMinimumSize(new Dimension(700,450));
		this.addWindowListener	(new WindowAdapter(){ 
			public void windowClosing(WindowEvent e){ 
				setVisible(false);				
			}	
		});	

		Dimension dialogSize = getSize();		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
		if(dialogSize.height > screenSize.height) 
			dialogSize.height = screenSize.height;
		if(dialogSize.width > screenSize.width)
			dialogSize.height = screenSize.width;
		setLocation((screenSize.width-dialogSize.width)/2,   
						(screenSize.height-dialogSize.height)/2);
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Imie", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		getContentPane().add(panel, gbc_panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		clientNameField = new JTextField();
		panel.add(clientNameField);
		clientNameField.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Nazwisko", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.insets = new Insets(0, 0, 5, 0);
		gbc_panel_1.gridx = 1;
		gbc_panel_1.gridy = 0;
		getContentPane().add(panel_1, gbc_panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		clientSurnameField = new JTextField();
		panel_1.add(clientSurnameField);
		clientSurnameField.setColumns(10);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Telefon Kontaktowy", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.insets = new Insets(0, 0, 5, 5);
		gbc_panel_2.gridx = 0;
		gbc_panel_2.gridy = 1;
		getContentPane().add(panel_2, gbc_panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		clientPhoneField = new JTextField();
		panel_2.add(clientPhoneField);
		clientPhoneField.setColumns(10);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Email", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.insets = new Insets(0, 0, 5, 0);
		gbc_panel_3.gridx = 1;
		gbc_panel_3.gridy = 1;
		getContentPane().add(panel_3, gbc_panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		clientMailField = new JTextField();
		panel_3.add(clientMailField);
		clientMailField.setColumns(10);
		
		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new TitledBorder(null, "PESEL", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.fill = GridBagConstraints.BOTH;
		gbc_panel_5.insets = new Insets(0, 0, 5, 5);
		gbc_panel_5.gridx = 0;
		gbc_panel_5.gridy = 2;
		getContentPane().add(panel_5, gbc_panel_5);
		panel_5.setLayout(new BorderLayout(0, 0));
		
		clientPinField = new JTextField();
		panel_5.add(clientPinField);
		clientPinField.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(null, "Numer i seria dowodu osobistego", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.fill = GridBagConstraints.BOTH;
		gbc_panel_4.insets = new Insets(0, 0, 5, 0);
		gbc_panel_4.gridx = 1;
		gbc_panel_4.gridy = 2;
		getContentPane().add(panel_4, gbc_panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		clientDocumentId = new JTextField();
		panel_4.add(clientDocumentId);
		clientDocumentId.setColumns(10);
		
		JPanel panel_7 = new JPanel();
		panel_7.setBorder(new TitledBorder(null, "Miejscowo\u015B\u0107", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_7 = new GridBagConstraints();
		gbc_panel_7.fill = GridBagConstraints.BOTH;
		gbc_panel_7.insets = new Insets(0, 0, 5, 5);
		gbc_panel_7.gridx = 0;
		gbc_panel_7.gridy = 3;
		getContentPane().add(panel_7, gbc_panel_7);
		panel_7.setLayout(new BorderLayout(0, 0));
		
		clientCityField = new JTextField();
		panel_7.add(clientCityField);
		clientCityField.setColumns(10);
		
		JPanel panel_9 = new JPanel();
		panel_9.setBorder(new TitledBorder(null, "Kod pocztowy", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_9 = new GridBagConstraints();
		gbc_panel_9.fill = GridBagConstraints.BOTH;
		gbc_panel_9.insets = new Insets(0, 0, 5, 0);
		gbc_panel_9.gridx = 1;
		gbc_panel_9.gridy = 3;
		getContentPane().add(panel_9, gbc_panel_9);
		panel_9.setLayout(new BorderLayout(0, 0));
		
		clientPostalCode = new JTextField();
		panel_9.add(clientPostalCode);
		clientPostalCode.setColumns(10);
		
		JPanel panel_8 = new JPanel();
		panel_8.setBorder(new TitledBorder(null, "Ulica", TitledBorder.LEADING, TitledBorder.ABOVE_TOP, null, new Color(59, 59, 59)));
		GridBagConstraints gbc_panel_8 = new GridBagConstraints();
		gbc_panel_8.gridwidth = 2;
		gbc_panel_8.fill = GridBagConstraints.BOTH;
		gbc_panel_8.insets = new Insets(0, 0, 5, 0);
		gbc_panel_8.gridx = 0;
		gbc_panel_8.gridy = 4;
		getContentPane().add(panel_8, gbc_panel_8);
		panel_8.setLayout(new BorderLayout(0, 0));
		
		clientStreetField = new JTextField();
		panel_8.add(clientStreetField);
		clientStreetField.setColumns(10);
		
		JPanel panel_6 = new JPanel();
		GridBagConstraints gbc_panel_6 = new GridBagConstraints();
		gbc_panel_6.anchor = GridBagConstraints.LAST_LINE_END;
		gbc_panel_6.gridwidth = 2;
		gbc_panel_6.insets = new Insets(0, 0, 0, 5);
		gbc_panel_6.gridx = 0;
		gbc_panel_6.gridy = 5;
		getContentPane().add(panel_6, gbc_panel_6);
		
		btnZamknij = new JButton("Zamknij");
		btnZamknij.addActionListener(this);
		panel_6.add(btnZamknij);
		
		
		btnZapisz = new JButton("Zapisz");
		btnZapisz.addActionListener(this);
		panel_6.add(btnZapisz);
		
		btnZapiszIZamknij = new JButton("Zapisz i Zamknij");
		btnZapiszIZamknij.addActionListener(this);
		panel_6.add(btnZapiszIZamknij);
		this.addWindowListener(this);
	}

	
	@Override
	public void done() {
		fillObjectWithData();
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transation = session.beginTransaction();
		session.saveOrUpdate(object);
		if(isObjectSet())
			objectSet = false;
		transation.commit();
		session.close();
		object = null;
		setEmptyField();
	}

	@Override
	public void setObject(Client object) {
		this.object = object;
		objectSet = true;

	}
	private void setEmptyField(){
		clientNameField.setText("");
		clientSurnameField.setText("");
		clientPhoneField.setText("");
		clientMailField.setText("");
		clientPinField.setText("");
		clientDocumentId.setText("");
		clientCityField.setText("");
		clientPostalCode.setText("");
		clientStreetField.setText("");
	}
	private void setInitialData(){
		String name = object.getFirstName();
		String surname = object.getLastName();
		String phone = object.getPhoneNumber();
		String mail = object.getMail();
		String pin = Long.toString(object.getPIN());
		String docid = object.getDocumentID();
		String postalCode = object.getPostalCode();
		String street = object.getStreet();
		String city = object.getCity();
		
		clientNameField.setText(name);
		clientSurnameField.setText(surname);
		clientPhoneField.setText(phone);
		clientMailField.setText(mail);
		clientPinField.setText(pin);
		clientDocumentId.setText(docid);
		clientCityField.setText(city);
		clientPostalCode.setText(postalCode);
		clientStreetField.setText(street);
	}
	
	private void fillObjectWithData(){
		if(!isObjectSet()){
		object = new Client();
		}
		try{
		object.setFirstName(clientNameField.getText());
		object.setLastName(clientSurnameField.getText());
		object.setPhoneNumber(clientPhoneField.getText());
		object.setMail(clientMailField.getText());
		object.setPIN(Long.parseLong(clientPinField.getText()));
		object.setDocumentID(clientDocumentId.getText());
		object.setCity(clientCityField.getText());
		object.setPostalCode(clientPostalCode.getText());
		object.setStreet(clientStreetField.getText());
		}catch(NullPointerException nul){
			
		}
		
		
	}
	@Override
	public boolean isObjectSet() {
		
		return objectSet;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnZamknij){
			object = null;
			objectSet = false;
			this.dispose();
		}
		else if(e.getSource()==btnZapisz){
			done();
			
		}
		else if(e.getSource()==btnZapiszIZamknij){
			done();
			objectSet = false;
			this.dispose();
		}
		
	}


	@Override
	public void showCreator() {
			if(isObjectSet()){
				setInitialData();
			}else{
				setEmptyField();
			}
			this.setVisible(true);
		
	}


	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowClosing(WindowEvent e) {
		ActionEvent action = new ActionEvent(btnZamknij, 0,null);
		actionPerformed(action);
		
	}


	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}